package com.itrip.itripbiz.controller;

import com.itrip.common.DtoUtil;
import com.itrip.common.EmptyUtils;
import com.itrip.common.ValidationToken;
import com.itrip.dto.Dto;
import com.itrip.entity.ItripComment;
import com.itrip.entity.ItripHotel;
import com.itrip.entity.ItripImage;
import com.itrip.entity.ItripUser;
import com.itrip.service.ItripCommentService;
import com.itrip.service.ItripHotelService;
import com.itrip.service.ItripLabelDicService;
import com.itrip.vo.ItripLabelDicVO;
import com.itrip.vo.comment.ItripAddCommentVO;
import com.itrip.vo.comment.ItripHotelDescVO;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@RestController
@RequestMapping("biz/api/comment/")
@Slf4j
public class SystemCommentController {
    @Resource
    private ValidationToken validationToken;
    @Resource
    private ItripCommentService itripCommentService;
    @Resource
    private RedisTemplate redisTemplate;
    @PostMapping("add")
    public Dto addComment(@RequestBody ItripAddCommentVO itripAddCommentVO, HttpServletRequest request){
        //ItripComment
        String tokenString = request.getHeader("token");
        ItripUser currentUser = (ItripUser) redisTemplate.opsForValue().get(tokenString);
        if(null != currentUser && null != itripAddCommentVO){
            //新增评论，订单id不能为空
            if(EmptyUtils.isEmpty(itripAddCommentVO.getOrderId()) ){
                return DtoUtil.returnFail("新增评论，订单ID不能为空","100005");
            }
            List<ItripImage> itripImages = null;
            ItripComment itripComment = new ItripComment();
            itripComment.setContent(itripAddCommentVO.getContent());
            itripComment.setHotelId(itripAddCommentVO.getHotelId());
            itripComment.setIsHavingImg(itripAddCommentVO.getIsHavingImg().longValue());
            itripComment.setPositionScore(BigDecimal.valueOf(itripAddCommentVO.getPositionScore()));
            itripComment.setFacilitiesScore(BigDecimal.valueOf(itripAddCommentVO.getFacilitiesScore()));
            itripComment.setHygieneScore(BigDecimal.valueOf(itripAddCommentVO.getHygieneScore()));
            itripComment.setOrderId(itripAddCommentVO.getOrderId());
            itripComment.setServiceScore(BigDecimal.valueOf(itripAddCommentVO.getServiceScore()));
            itripComment.setProductId(itripAddCommentVO.getProductId());
            itripComment.setProductType(itripAddCommentVO.getProductType().longValue());
            itripComment.setIsOk(itripAddCommentVO.getIsOk().longValue());
            itripComment.setTripMode( Long.parseLong(itripAddCommentVO.getTripMode()));
            itripComment.setCreatedBy(currentUser.getId());
            itripComment.setCreationDate(new Date(System.currentTimeMillis()));
            itripComment.setUserId(currentUser.getId());
            try {
                if(itripAddCommentVO.getIsHavingImg() == 1 ){
                    itripImages = new ArrayList<ItripImage>();
                    //loop input imgs array
                    int i =1;
                    for (ItripImage itripImage : itripAddCommentVO.getItripImages()) {
                        itripImage.setPosition(Long.valueOf(i));
                        itripImage.setCreatedBy(currentUser.getId());
                        itripImage.setCreationDate(itripComment.getCreationDate());
                        itripImage.setType("2");
                        itripImages.add(itripImage);
                        i++;
                    }
                }

                itripCommentService.itriptxAddItripComment(itripComment,(null == itripImages?new ArrayList<ItripImage>():itripImages));
                return  DtoUtil.returnSuccess("新增评论成功");
            } catch (Exception e) {
                e.printStackTrace();
                return  DtoUtil.returnFail("新增评论失败","100003");
            }
        }else if(null != currentUser && null == itripAddCommentVO){
            return  DtoUtil.returnFail("不能提交空，请填写评论信息","100004");
        }else{
            return  DtoUtil.returnFail("token失效，请重登录","100000");
        }

    }
    @Resource
    private ItripLabelDicService itripLabelDicService;
    @GetMapping("gettraveltype")
    public Dto getTravelType(){
        try {
            List<ItripLabelDicVO> itripLabelDicByParentId = itripLabelDicService.getItripLabelDicByParentId();
            return  DtoUtil.returnDataSuccess(itripLabelDicByParentId);
        } catch (Exception e) {
            e.printStackTrace();
            return  DtoUtil.returnFail("获取旅游类型列表错误","100019");
        }
    }
    @Resource
    private ItripHotelService itripHotelService;
    @GetMapping("gethoteldesc/{hotelId}")
    @ResponseBody
    public Dto<Object> getHotelDesc(@ApiParam(required = true, name = "hotelId", value = "酒店ID")
                                    @PathVariable String hotelId){
        Dto<Object> dto = new Dto<Object>();
        log.debug("hotelId : " + hotelId);
        ItripHotelDescVO itripHotelDescVO = null;
        try{
            if(null != hotelId && !"".equals(hotelId)){
                ItripHotel itripHotel = new ItripHotel();
                itripHotel = itripHotelService.chaxunjuediani(Long.valueOf(hotelId));
                itripHotelDescVO = new ItripHotelDescVO();
                itripHotelDescVO.setHotelId(itripHotel.getId());
                itripHotelDescVO.setHotelName(itripHotel.getHotelName());
                itripHotelDescVO.setHotelLevel(itripHotel.getHotelLevel().intValue());
            }
            dto = DtoUtil.returnDataSuccess(itripHotelDescVO);
        }catch (Exception e){
            e.printStackTrace();
            dto = DtoUtil.returnFail("获取酒店相关信息错误","100021");
        }

        return dto;
    }
}
